package org.example.netty.simple;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


/**
 * @author 邓天天
 * @date 2021/4/8 22:18
 */
public class NettyClient {
    public static void main(String[] args) {
        final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try{// 创建客户端启动对象
            final Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(eventLoopGroup)
                    // 设置客户端通道实现类
                    .channel(NioSocketChannel.class)
                    // 设置客户段通道实现类
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            // 加入自己的处理器
                            socketChannel.pipeline().addLast(new NettyClientHandler());
                        }
                    });
            System.out.println("客户端 OK！");
            final ChannelFuture future = bootstrap.connect("127.0.0.1", 10010).sync();

            future.channel().closeFuture().sync();
        }catch (InterruptedException exception){
            exception.printStackTrace();
        }finally {
            eventLoopGroup.shutdownGracefully();
        }



    }
}
